/**
 * Created by dragon on 2015/12/10.
 */
(function(factory){
    if(typeof define === 'function' && define.amd){
        define(['jquery','../../../Common/catedb','select'],factory);
    }else{
        factory(window.jQuery);
    }
}(function($){
    /*资讯分类二级联动=====================================*/
    $.fn.articlecate = function(o) {
        var that = $.extend({},$.fn.articlecate.defaults, o);
        return this.each(function() {
            var t = $(this);
            var arthtml='<select name="'+that.a+'" id="'+that.a+'" class="'+that.a+'_item mr10"><option value="">请选择一级分类</option></select>';
            arthtml += '<select name="'+that.b+'" id="'+that.b+'" class="'+that.b+'_item mr10"><option value="">二级分类</option></select>';

            t.html(arthtml);
            var first=t.find("."+that.a+"_item");
            var second=t.find("."+that.b+"_item");

            var ThatData=LocalData['articlecate'][0];
            var tdataB=[];
            for(var i in ThatData){
                first.append('<option value="'+ThatData[i].path+'" data-path="'+ThatData[i].path+'">'+ThatData[i].title+'</option>');
            }

            function gethtml(id){
                tdataB=ThatData[id].child;
                for(var i in tdataB){
                    second.append('<option value="'+tdataB[i].id+'" data-path="'+tdataB[i].path+'">'+tdataB[i].title+'</option>');
                }
            };

            function selects(){
                $(that.input).val('');

                if(second.val() && (typeof(tdataB[second.val()].child)=='undefined')){
                    var c=second.find('option:selected');
                    $(that.input).val(c.data('path'));
                    return false;
                }

                if(first.val() && that.minSelectLevel <= 1){
                    var c=first.find('option:selected');
                    $(that.input).val(c.data('path'));
                    return false;
                }
            };

            initdb();
            /*初始化数据===============================*/
            function initdb(){
                var input = $(that.input).val();
                var idsarr=[];
                if(input != ''){
                    idsarr = input.split("-");

                    if(idsarr.length > 0){
                        first.find('[value="'+idsarr[0]+'"]').attr('selected',true);
                        gethtml(idsarr[0]);
                    }

                    if(idsarr.length > 1){
                        second.find('[value="'+idsarr[1]+'"]').attr('selected',true);
                    }
                    selects();
                }
            };
            var _first,_second;
            $(first).selectbox({w:(that.width?that.width[0]:''),cutstr:(that.cutstr?that.cutstr[0]:''),divclass:that.sclass,callback:function(t){
                second.children(':not(:first)').remove();
                _second.init();
                var path=t.attr('v');
                if(path==='') return false;
                gethtml(path);
                selects();
                _second.init();
            }},function(){
                _first=this;
            });
            $(second).selectbox({w:(that.width?that.width[1]:''),cutstr:(that.cutstr?that.cutstr[1]:''),divclass:that.sclass,callback:function(t){
                selects();
            }},function(){
                _second=this;
            });
        });
    };
    $.fn.articlecate.defaults = {a:'first', b:'second',minSelectLevel:2,sclass:''};
}));